package service

import (
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"shrimp-blog-server/app/dao"
	"shrimp-blog-server/app/model"
)

func (*MessagesService) AddMessage(req *ghttp.Request, message ...model.Messages) {
	if len(message) == 0 {
		responses.ErrorResponseExit(req, "留言信息不能为空")
	}
	result, err := dao.Messages.Insert(message...)
	if err != nil {
		g.Log("添加留言失败").Error(err)
		panic(gerror.Wrap(err, "添加留言失败"))
	}
	if result == 0 {
		responses.ErrorResponseExit(req, "添加留言失败")
	}
	responses.SuccessResponse(req)
}

func (*MessagesService) GetModelsByPage(req *ghttp.Request, page, pageSize int) {
	messages, err := dao.Messages.GetModelsByPage(page, pageSize)
	if err != nil {
		g.Log("添加留言失败").Error(err)
		panic(gerror.Wrap(err, "添加留言失败"))
	}
	responses.Json(req, successCode, successMessage, messages)
}

func (*MessagesService) GetModelById(req *ghttp.Request, id ...uint) {
	if len(id) == 0 {
		responses.ErrorResponseExit(req, "id不能为空")
	}

	message, err := dao.Messages.GetModelById(id...)
	if err != nil {
		g.Log("添加留言失败").Error(err)
		panic(gerror.Wrap(err, "添加留言失败"))
	}
	responses.Json(req, successCode, successMessage, message)
}
